/*
 * @(#)DurationFormat.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.text.Format;
import java.util.Calendar;
import java.util.Locale;

/**
 * DurationFormat.java
 *
 * DurationFormat is an abstract class for duration formatting.<br>
 * Sample Usage:<br>
 * DurationFormat df = new DurationFormat ( new MessageFormat ( "{1}y {2}M {3}d {4}h {5}m {0,choice,-1#To Go|1#Ago}" ) );<br>
 * df.format ( duration );
 * <br><br>
 * TODO: Implement format base on different duration value
 *   @author            Lun Au
 *   @company       DCIVision Limited
 *   @creation date  26/09/2006
 *   @version          $Revision: 1.1 $
 */

public class DurationFormat {

    /**
     * Index indicator for the original total duration in milliseconds
     */
    public static final int DURATION_INDEX_ORIGIN_MILLIS = 0;

    /**
     * Index indicator for the duration in years
     */
    public static final int DURATION_INDEX_YEAR = 1;

    /**
     * Index indicator for the duration in months
     */
    public static final int DURATION_INDEX_MONTH = 2;

    /**
     * Index indicator for the duration in days
     */
    public static final int DURATION_INDEX_DAY = 3;

    /**
     * Index indicator for the duration in hours
     */
    public static final int DURATION_INDEX_HOUR = 4;

    /**
     * Index indicator for the duration in minutes
     */
    public static final int DURATION_INDEX_MINUTE = 5;

    /**
     * Index indicator for the duration in seconds
     */
    public static final int DURATION_INDEX_SECOND = 6;

    /**
     * Index indicator for the duration in milliseconds
     */
    public static final int DURATION_INDEX_MILLIS = 7;

    protected Format format = null;

    /**
     * @param format
     */
    public DurationFormat ( Format format ) {
        this.format = format;
    }

    /**
     * The pattern of display duration in years
     * @param locale 
     * @return The years pattern
     */
    public static String getYearPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.year");
    }

    /**
     * The pattern of display duration in months
     * @param locale 
     * @return The months pattern
     */
    public static String getMonthPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.month");
    }

    /**
     * The pattern of display duration in days
     * @param locale 
     * @return The days pattern
     */
    public static String getDayPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.day");
    }

    /**
     * The pattern of display duration in hours
     * @param locale 
     * @return The hours pattern
     */
    public static String getHourPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.hour");
    }

    /**
     * The pattern of display duration in minutes
     * @param locale 
     * @return The minutes pattern
     */
    public static String getMinutePattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.minute");
    }

    /**
     * The pattern of display duration in seconds
     * @param locale 
     * @return The seconds pattern
     */
    public static String getSecondPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.second");
    }

    /**
     * The pattern of display duration in milliseconds
     * @param locale 
     * @return The millisecond pattern
     */
    public static String getMillisPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.millis");
    }

    /**
     * The description pattern, for example, if start date is before end date, it's "Ago", otherwise it's "To Go"
     * @param locale 
     * @return The description pattern
     */
    public static String getDescPattern ( Locale locale ) {
        return MessageResourcesFactory.getMessage(locale, "common.duration.pattern.desc");
    }

    /**
     * Formats the duration
     * @param duration The duration object to be formatted
     * @return The formatted duration string
     * @paradm duration The duration object
     */
    public String format ( Duration duration ) {
        Object values = new Object[]{
                                    new Long(duration.getMillisDiff()),
                                    new Long(duration.get(Calendar.YEAR)),
                                    new Long(duration.get(Calendar.MONTH)),
                                    new Long(duration.get(Calendar.DAY_OF_MONTH)),
                                    new Long(duration.get(Calendar.HOUR_OF_DAY)),
                                    new Long(duration.get(Calendar.MINUTE)),
                                    new Long(duration.get(Calendar.SECOND)),
                                    new Long(duration.get(Calendar.MILLISECOND))
                                };
        return format.format (values);
    }
}
